package com.neolix.watch.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.neolix.watch.R;
import com.neolix.watch.util.FontTypefaceUtil;
import com.neolix.watch.util.StringUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import static com.neolix.watch.domain.BaiDuWeatherResult.BaiDuWeatherData;
import static com.neolix.watch.domain.WeatherType.findCommandType;
import static com.neolix.watch.util.DateTimeUtil.isDay;
import static com.neolix.watch.util.StringUtil.createDrawableUrl;
import static com.neolix.watch.util.StringUtil.formatTimeToMonthAndDayString2;

public class WeatherItemView extends LinearLayout {

    private ImageView weatherImage;
    private TextView currentTemperature;
    private TextView currentDate;
    private TextView currentWeek;

    private DisplayImageOptions options;
    private ImageLoader imageLoader;

    public WeatherItemView(Context context) {
        super(context);
        initUi();
    }

    public WeatherItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initUi();
    }

    public WeatherItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initUi();
    }

    protected void initUi() {
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View container = inflater.inflate(R.layout.view_weather_item, this, true);

        weatherImage = (ImageView) container.findViewById(R.id.weather_condition_imageView);
        currentTemperature = (TextView) container.findViewById(R.id.current_temperature);
        currentWeek = (TextView) container.findViewById(R.id.current_week);
        currentDate = (TextView) container.findViewById(R.id.current_date);

        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        imageLoader = ImageLoader.getInstance();

        FontTypefaceUtil.setTypefaceOfHelvetica(currentTemperature);
    }

    public void setModel(BaiDuWeatherData weatherData) {
//        imageLoader.displayImage(isDay() ? weatherData.getDayPictureUrl()
//                : weatherData.getNightPictureUrl(), weatherImage, options);
        imageLoader.displayImage(isDay() ? createDrawableUrl(findCommandType(weatherData.getWeather()).getDaySmallPictureId()) :
                        createDrawableUrl(findCommandType(weatherData.getWeather()).getNightSmallPictureId()),
                weatherImage, options
        );
        currentTemperature.setText(weatherData.getTemperature());
        currentWeek.setText(weatherData.getDate());
        currentDate.setText(formatTimeToMonthAndDayString2(weatherData.getDateTime()));
    }
}
